package dog.gui;

import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.*;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import constants.CHANGE;
import containers.Person;
import dog.gui.calendar.CalendarPanel;
import dog.model.Model;

@SuppressWarnings("serial")
public class PersonList extends JPanel implements PropertyChangeListener, ListSelectionListener
{

	JList list;
	Model model;
	JTextField searchField;
	CalendarPanel cp = null;
	
	public PersonList(final Model model,CalendarPanel panel)
	{
		this.cp=panel;
		this.model = model;
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		searchField = new JTextField(10);
		searchField.addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent arg0) {}
			public void keyTyped(KeyEvent arg0) {}
			public void keyReleased(KeyEvent arg0) {
				buildList(model.gui_getCurrentPersons(),searchField.getText());
			}});
		c.gridx = 0;
		c.gridy = 0;
		add(searchField, c);

		model.addPropertyChangeListener(this);
		list = new JList(new DefaultListModel());
		list.addListSelectionListener(this);
		list.setCellRenderer(new PersonCellRenderer());
		list.setSelectionMode(DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		list.setPrototypeCellValue(new Person(-1, -1, "Aaaaaaaaaa@Aaaaaaaaaa.aaa", "Aaaaaaaaaa Aaaaaaaaaa"));
		Person[] curr = model.gui_getCurrentPersons();
		buildList(curr,"");
		c.gridy = 1;
		add(new JScrollPane(list), c);
		//add(list);
	}
	@Override
	public void propertyChange(PropertyChangeEvent e)
	{
		String type = e.getPropertyName();
		if (type.equals(CHANGE.PERSON))
		{
			Person old = (Person)e.getOldValue();
			Person person = (Person)e.getNewValue();
			DefaultListModel listModel = (DefaultListModel)list.getModel();
			if (old!=null)
				listModel.removeElement(old);
			if (person!=null && personPassesFilter(person,searchField.getText()))
				listModel.addElement(person);
		}
	}
	
	public void buildList(Person[] persons,String filter) 
		{
		((DefaultListModel)list.getModel()).removeAllElements();
		filter = filter.toLowerCase();
		for(int i=0; i < persons.length; i++)
			{
			if (personPassesFilter(persons[i],filter)) 
				{
				((DefaultListModel)list.getModel()).addElement(persons[i]);
				}
			}
		}
	private boolean personPassesFilter(Person p, String f)
		{
		return (p.realName.toLowerCase().contains(f) || p.email.toLowerCase().contains(f));
		}
	public int[] getSelectedIDs()
		{
		DefaultListModel listModel = (DefaultListModel)list.getModel();
		ArrayList<Integer> ids = new ArrayList<Integer>();
		for(int i=0; i<listModel.size(); i++)
			{
			if (list.isSelectedIndex(i))
				{
				ids.add(((Person)listModel.getElementAt(i)).personID);
				}
			}
		int[] result = new int[ids.size()];
		for(int i=0; i<ids.size(); i++)
			result[i]=ids.get(i);
		return result;
		}
	@Override
	public void valueChanged(ListSelectionEvent e)
		{
		if(cp!=null)
			{
			DefaultListModel listModel = (DefaultListModel)list.getModel();
			boolean empty=true;
			for(int i=0; i<listModel.size(); i++)
				{
				if (list.isSelectedIndex(i))
					{
					cp.IsInSelection(((Person)listModel.get(i)).personID,true);
					empty=false;
					}
				else
					cp.IsInSelection(((Person)listModel.get(i)).personID,false);
				}
			if(empty)//Default to viewing only yourself
				model.gui_setAccessPerson(model.getPersonID());
			}
		}
	}